// pages/my/items/items.js
import {
  request
} from '../../../request/index'
Page({
  data: {
    nickName: '',
    phone: '',
    email: '',
    birthday: '',
    fileList: [],
    gender: '',
    show: false,
    currentDate: new Date().getTime(),
    minDate: new Date(1990, 1, 1).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    option2: [{
        text: '男',
        value: '0'
      },
      {
        text: '女',
        value: '1'
      },
      {
        text: '保密',
        value: '2'
      },
    ],
    value: 3,
  },

  //删除图片
  deleteImg() {
    this.setData({
      fileList: []
    });
  },

  // 在选择文件后触发
  afterRead(event) {
    const token = wx.getStorageSync('token');
    const {
      file
    } = event.detail;
    let that = this
    //回显图片,设置为正在上传
    that.setData({
      fileList: [{
        url: file.url,
        name: '文章封面',
        status: 'uploading',
        message: '上传中',
      }]
    })
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: 'http://localhost:8080/upload',
      filePath: file.url, //传递的url
      name: 'file', //后端参数名
      header: {
        "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
        "author": token ? token : '',
      },
      formData: {},
      success: function (res) {
        let data = res.data;
        try {
          data = JSON.parse(data);
          console.log(data.data);
          that.setData({
            fileList: [{
              url: data.data,
              name: '头像',
            }]
          })
        } catch (error) {
        }
      },
    })
  },

  showPopup() {
    this.setData({
      show: true
    });
  },

  onClose() {
    this.setData({
      show: false
    });
  },

  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },

  onConfirm(event) {
    this.setData({
      show: false,
      birthday: this.formatDate(new Date(event.detail))
    });
  },

  formatDate(date) {
    let taskStartTime
    if (date.getMonth() < 9) {
      taskStartTime = date.getFullYear() + "-0" + (date.getMonth() + 1) + "-"
    } else {
      taskStartTime = date.getFullYear() + "-" + (date.getMonth() + 1) + "-"
    }
    if (date.getDate() < 10) {
      taskStartTime += "0" + date.getDate()
    } else {
      taskStartTime += date.getDate()
    }
    // taskStartTime += " " + date.getHours() + ":" + date.getMinutes()
    // this.setData({
    //   taskStartTime: taskStartTime,
    // })
    return taskStartTime;
  },

  save() {
    const token = wx.getStorageSync('token');
    let data = {
      nickName: this.data.nickName,
      phone: this.data.phone,
      email: this.data.email,
      birthday: this.data.birthday,
      pic: this.data.fileList[0].pic ? this.data.fileList[0].pic : '',
      // gender: this.data.option2[this.data.value].text,
    }
    request({
      url: '/user/update',
      method: 'PUT',
      data: data,
      header: {
        'author': token ? token : '',
      }
    }).then(res => {
      if (res.code === 0) {
        wx.showToast({
          title: '成功',
          icon: 'success'
        })
      } else {
        wx.showToast({
          title: '未知错误',
          icon: 'error'
        })
      }
    })
  },

  onLoad() {
    const token = wx.getStorageSync('token');
    request({
      url: '/user/userInfo',
      method: 'GET',
      header: {
        'author': token ? token : '',
      }
    }).then(res => {
      if (res.code === 0) {
        this.setData({
          nickName: res.data.nickName,
          phone: res.data.phone,
          email: res.data.email,
          birthday: res.data.birthday,
          pic: res.data.pic,
        })
        if (!res.data.gender) {
          this.setData({
            value: 3
          })
        } else if (res.data.gender === '男') {
          this.setData({
            value: 0
          })
        } else if (res.data.gender === '女') {
          this.setData({
            value: 1
          })
        } else if (res.data.gender === '保密') {
          this.setData({
            value: 3
          })
        }
      } else {
        wx.showToast({
          title: '未知错误',
          icon: 'error'
        })
      }
    })
  }
})